/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.woxthebox.draglistview.sample.adapter;

import com.woxthebox.draglistview.basecomponent.BaseDependentLayout;
import com.woxthebox.draglistview.basecomponent.BaseDirectionalLayout;
import com.woxthebox.draglistview.sample.ResourceTable;
import com.woxthebox.draglistview.sample.bean.HorizontalBean;
import com.woxthebox.draglistview.sample.untils.DataUntils;
import com.woxthebox.draglistview.utils.FinalStaticBean;
import com.woxthebox.draglistview.utils.ToastUtil;

import ohos.aafwk.ability.AbilitySlice;
import ohos.agp.components.BaseItemProvider;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.DependentLayout;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.ListContainer;
import ohos.agp.components.TableLayoutManager;
import ohos.agp.components.Text;
import ohos.agp.window.service.Display;
import ohos.agp.window.service.DisplayManager;

import java.util.List;

/**
 * 横向滑动的List的适配器
 *
 * @since 2021-04-19
 */
public class HorizontalListAdapter extends BaseItemProvider {
    private List<HorizontalBean> list;
    private AbilitySlice slice;
    private Display display;
    private int mLayoutType;
    private boolean isEnable = true;

    /**
     * HorizontalListAdapter构造器
     *
     * @param list
     * @param slice
     * @param mlayoutType
     */
    public HorizontalListAdapter(List<HorizontalBean> list, AbilitySlice slice, int mlayoutType) {
        this.slice = slice;
        this.mLayoutType = mlayoutType;
        this.list = list;
        display = DisplayManager.getInstance().getDefaultDisplay(slice).get();
    }

    @Override
    public int getCount() {
        return list == null ? 0 : list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int id) {
        return id;
    }

    public void setEnable(boolean isMenable) {
        this.isEnable = isMenable;
    }

    @Override
    public Component getComponent(int i, Component component, ComponentContainer componentContainer) {
        BaseItemProvider itemProvider = null;
        component = null;
        Component cpt;
        HorizontalViewHolder viewHolder;
        if (component == null) {
            cpt = LayoutScatter.getInstance(slice).parse(ResourceTable.Layout_horizontal_item_layout,null,false);
            cpt.setWidth((int) (display.getAttributes().width * FinalStaticBean.DISPLAYWIDTH));
            viewHolder = new HorizontalViewHolder(cpt);
            cpt.setTag(viewHolder);
        } else {
            cpt = component;
            viewHolder = (HorizontalViewHolder) cpt.getTag();
        }
        viewHolder.mShowTitle.setText("Column " + list.get(i).getIndex());
        viewHolder.mShowFoolter.setText("Column " + list.get(i).getIndex() + " footer");
        viewHolder.mListSizeNum.setText("" + list.get(i).getListBeans().size());
        viewHolder.mListContainer.setLongClickable(isEnable);
        if (mLayoutType == FinalStaticBean.HORIZONTALLISTLAYOUT) {
            itemProvider = new ListAdapter(list.get(i).getListBeans(),slice, FinalStaticBean.RANDIUBORDER, null);
            ((ListAdapter)itemProvider).setEnable(isEnable);
            viewHolder.mListContainer.setItemProvider(itemProvider);
        } else if (mLayoutType == FinalStaticBean.HORIZONTALGRIDLAYOUT) {
            TableLayoutManager tableLayoutManager = new TableLayoutManager();
            tableLayoutManager.setColumnCount(FinalStaticBean.NUM_4);
            viewHolder.mListContainer.setLayoutManager(tableLayoutManager);
            itemProvider = new GridAdapter(list.get(i).getListBeans(), slice, FinalStaticBean.HORIZONTALGRIDLAYOUT);
            ((GridAdapter)itemProvider).setEnable(isEnable);
            viewHolder.mListContainer.setItemProvider(itemProvider);
        }
        BaseItemProvider finalItemProvider = itemProvider;
        viewHolder.mDependentLayout.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                DataUntils.addListHorizontalBean(list.get(i).getListBeans());
                finalItemProvider.notifyDataChanged();
                notifyDataChanged();
            }
        });
        viewHolder.mListContainer.setItemLongClickedListener(new ListContainer.ItemLongClickedListener() {
            @Override
            public boolean onItemLongClicked(ListContainer listContainer, Component component, int i, long l) {
                if (component instanceof BaseDependentLayout) {
                    ((BaseDependentLayout)component).onLongClicked(component);
                } else if (component instanceof BaseDirectionalLayout) {
                    ((BaseDirectionalLayout)component).onLongClicked(component);
                }
                return true;
            }
        });
        viewHolder.mListContainer.setItemClickedListener(new ListContainer.ItemClickedListener() {
            @Override
            public void onItemClicked(ListContainer listContainer, Component component, int i, long l) {
                ToastUtil.toast(slice, "Item clicked");
            }
        });
        return cpt;
    }

    /**
     * HorizontalListAdapter的ViewHolder
     *
     * @since 2021-04-19
     */
    private class HorizontalViewHolder {
        Text mShowTitle;
        Text mListSizeNum;
        Text mShowFoolter;
        ListContainer mListContainer;
        DependentLayout mDependentLayout;

        HorizontalViewHolder(Component component) {
            mShowTitle = (Text) component.findComponentById(ResourceTable.Id_item_show_title);
            mListSizeNum = (Text) component.findComponentById(ResourceTable.Id_item_listsize);
            mListContainer = (ListContainer) component
                    .findComponentById(ResourceTable.Id_horizontal_item_listcontainer);
            mShowFoolter = (Text) component.findComponentById(ResourceTable.Id_horizontal_foolter);
            mDependentLayout = (DependentLayout) component
                    .findComponentById(ResourceTable.Id_item_show_dependentLayout);
        }
    }
}
